<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/global.js"></script>
		<script src="../js/checked.js"></script>
	</head>
	<body>
		<form id="formCreateUser" class="layui-form" action="" onsubmit="return false">
			<div class="layui-form layuimini-form">
				<div class="layui-form-item">
					<label class="layui-form-label required">试卷名称</label>
					<div class="layui-input-block">
						<input type="text" name="testPaperName" lay-verify="required" lay-reqtext="试卷名称不能为空" placeholder="请输入试卷名称" value=""
						 class="layui-input">
						<tip>格式xxxx年-xxxx年xxxx考试</tip>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label required">考试时长</label>
					<div class="layui-input-block">
						<input type="number" name="number" lay-verify="required" lay-reqtext="考试时长不能为空" placeholder="考试时长" value="" class="layui-input">
					</div>
				</div>
              
			  <div class="layui-form-item">
			  	<label class="layui-form-label">科目:</label>
			  	<div class="layui-input-block">
			  		<select class="su"  id="ssubject" name="subjectId">
			  			<option></option>
			  		</select>
			  	</div>
			  </div>
				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label">试卷备注信息</label>
					<div class="layui-input-block">
						<textarea name="testPaperNotes" class="layui-textarea" placeholder="请输入试卷备注信息"></textarea>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label required">选择试题</label>
					<div class="layui-input-block">
						<button class="layui-btn " id="hand" style="color: black;background-color: #2D93CA">手动添加试题</button>
						<img id="shoudong" style="width: 40px;height: 40px;display: none;" src="../images/rig.jpeg" />
						<button class="layui-btn" id="auto" style="background-color: #46B8DA;margin-left: 120px;" lay-filter="quto">自动组卷</button>
						<img id="autoimg" style="width: 40px;height: 40px;display: none;" src="../images/rig.jpeg" />
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
					</div>
				</div>
			</div>
		</form>
		<!-- <input id="uidsub" class="form-control" name="uid" type="hidden" value="" >
		<form class="layui-form" action="" onsubmit="return false" lay-filter="updateForm">
			<div class="layui-form-item" style="margin-top: 30px;">
				<label class="layui-form-label">试卷名称</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input id="testPaperName" type="text"  name="testPaperName" class="layui-input">
					
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">试卷状态</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input id="status" type="text"  name="status"  class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">考试时长</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input id="testPaperTime" type="number" name="testPaperTime" lay-verify="required" 
					 class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				
				<div class="layui-input-inline" style="margin-left: 120px;" >
				<button class="layui-btn " id="hand" style="color: black;background-color: #2D93CA"  >手动添加试题</button><br>
				<span>dga</span>
				</div>
				<div class="layui-input-inline" >
				<button class="layui-btn"  style="background-color: #46B8DA" lay-filter="addQuestion">自动组卷</button>
				</div>
			</div>
			<div class="layui-input-inline" style="text-align: center;">
				
			</div>
			<div class="layui-btn-container" style="text-align: center;">
				<button class="layui-btn" lay-submit="" lay-filter="addQuestion">添加</button>
			</div>
		</form> -->
		<!-- 自动组卷 -->
		<div id="updatePage" style="display: none;">
			<form class="layui-form" action="" onsubmit="return false" lay-filter="updateForm">
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span>科&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 目: </label>
					<div class="layui-input-inline" style="width: 350px;">
						<select class="su" id="asubjectId" name="subjectId">
							<option></option>
						</select>
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span>难&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 度: </label>
					<div class="layui-input-inline" style="width: 350px;">
						<select id="aquestionGrade" name="questionGrade">
							<option value="0">偏易</option>
							<option value="1">适中</option>
							<option value="2">偏难</option>
						</select>
					</div>
				</div>

				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span>试卷总分: </label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="number" id="atotalScore"  name="totalScore" value="0" min="0" class="layui-input" />
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span ><span class="val">单选</span>题:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="number" id="achoose"  name="choose" value="0" min="0" class="layui-input d l" />
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span ><span class="val">填空</span>题: </label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="number" id="acompletion"   name="completion" value="0" min="0" class="layui-input d l" />
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span ><span class="val">判断</span>题:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="number"  id="ajudgment" name="judgment"  value="0" min="0" class="layui-input d l" />
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-mid"><span style="color: red;">*</span><span class="val">简答</span>题: </label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="number" id="aschoolProfile"   name="schoolProfile" value="0" min="0" class="layui-input d l" />
					</div>
				</div>
				
			</form>
		</div>
		<script>
			var subjectId;
			layui.use(['table', 'form', 'layer', 'laydate', 'upload', 'element', 'jquery'], function() {
				var layer = layui.layer;
				var form = layui.form;
				var table = layui.table;
				var laydate = layui.laydate;
				var upload = layui.upload;
				var element = layui.element;
				var jquery = layui.jquery
				var $ = layui.$;

				//渲染试题类型表
				$.get(globalData.examinationServer + '/questionType/listQuestionTypes', function(res) {
					var html = "";
					for (var i = 0; i < res.data.length; i++) {
						//html += "<input type="radio" name="${index}" value="res.data[i].questionTypeId+" title="${taoZimu}" $
						html += "<option  value=" + res.data[i].questionTypeId + ">" + res.data[i].questionTypeName + "</option>"

					}
					$(".qu").append(html);

					form.render('select')
				}, "json")
				//渲染科目表
				$.get(globalData.examinationServer + '/subject/listSubjects', function(res) {
					var html = "";

					for (var i = 0; i < res.data.length; i++) {

						//html += "<input type="radio" name="${index}" value="res.data[i].questionTypeId+" title="${taoZimu}" $
						html += "<option  value=" + res.data[i].subjectId + ">" + res.data[i].subjectName + "</option>"

					}
					$(".su").append(html);

					form.render('select')
				}, "json")
				//添加按钮提交监听
				form.on('submit(saveBtn)', function(obj) {
					var subject=null;
					var url;
					var ids=globalData.getIds();
					
					if (null == ids || ids.length ==0) {
						subject=$("#asubjectId").val();
						url = globalData.examinationServer + '/testPaper/insertTestPaper?ids=' +"null";
					} else {
						subject=$("#ssubject").val();
						url = globalData.examinationServer + '/testPaper/insertTestPaper?ids=' + globalData.getIds();
					}


					var str = $("#formCreateUser").serialize();
					console.log(obj.field);
					$.ajax({
						type: "post",
						url: url,
						data: {
							testPaperName: obj.field.testPaperName,
							testPaperTime: obj.field.testPaperTime,
							testPaperNotes: obj.field.testPaperNotes,
							subjectId: subject,
							questionGrade: $("#aquestionGrade").val(),
							totalScore: $("#totalScore").val(),
							choose: $("#achoose").val(),
							completion: $("#acompletion").val(),
							judgment: $("#ajudgment").val(),
							schoolProfile: $("#aschoolProfile").val()
						}, //需要传递到服务端的数据
						dataType: 'json', //服务端响应数据类型
						xhrFields: {
							withCredentials: true
						},
						
						// beforeSend: function(request) {
						// 	request.setRequestHeader("token", globalData.getCurToken());
						// },
						success: function(res) {
							if (res.code == 0) {
								layer.msg('添加成功', {
									icon: 1,
									time: 2000
								}, function(index) {
									layer.close(index);
									//关闭父窗口并重载
									dkm._close();
									dkm._reload();
								});

							} else {
								layer.msg('添加失败', {
									icon: 2,
									time: 2000
								});
							}
						}
					});

					return false;
				});
				//点击手动组卷
				$("#hand").click(function() {
					layer.open({
						type: 2,
						title: '添加试题',
						area: ['1000px', '500px'],
						content: 'question.html',
						btn: ["确定", '关闭'],
						btn1: function(index, layero) {

							layer.msg("您成功添加了试题");
							layer.close(index);
							$("#shoudong").css("display", "");
						},
						btn2: function(index, layero) {
							layer.msg("您取消了添加试题")
							$("#shoudong").css("display", "none");


						}
					});
				})
                //自动组卷按钮
				$("#auto").click(function() {
					var update_index = layer.open({
						type: 1,
						title: '自动组卷信息',
						area: ['600px', '600px'],
						content: $('#updatePage'),
						btn: ["确定", '关闭'],
						btn1: function(index, layero) {
                             
							layer.msg("您成功添加了试题");
                              
							layer.close(index);
                             $("#autoimg").css("display", "");

						},
						btn2: function(index, layero) {
							layer.msg("您取消了添加试题")
							$("#autoimg").css("display", "none");
						}
					});
				})
				//失去焦点时获取数据库中对应的题数
				$(".d").blur(function(){
					var val=$(this).attr("name");
					var subjectId=$("#asubjectId").val();
					var  num=$(this).val();
					
					$.ajax({
						type: "post",
						url:globalData.examinationServer + '/question/getSubjectandNameCount',
						data: {
							subjectId: $("#asubjectId").val(),
							questionTypeName: val,
						}, //需要传递到服务端的数据
						dataType: 'json', //服务端响应数据类型
						xhrFields: {
							withCredentials: true
						},
						success: function(res) {
							if(num>res.code){
								
								
								layer.msg("该科目下本题型不足，请添加");
								
								
							}else{
								
							}
						}
					});
					
					
					
				})
				
			});
			
			
		</script>
	</body>
</html>
